﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QuTouTiaoAssist
{
    public class ControlTextValidate
    {
        public System.Windows.Forms.Control ValidateControl;
        public Func<string, bool> ValidateFunc;
        public string ErrorMsg;
        private bool m_isTrimText;
        private bool m_notFuncIsValid;

        public ControlTextValidate(Control control, Func<string, bool> func, string errorMsg, bool isTrimText):
            this(control,func,errorMsg,isTrimText, false)
        {
            
        }

        public ControlTextValidate(Control control, Func<string, bool> func, string errorMsg, bool isTrimText, bool notFuncIsValid)
        {
            this.ValidateControl = control;
            this.ValidateFunc = func;
            this.ErrorMsg = errorMsg;
            this.m_isTrimText = isTrimText;
            this.m_notFuncIsValid = notFuncIsValid;
        }

        public bool IsValid()
        {
            if (ValidateFunc != null && ValidateControl!=null)
            {
                string str = ValidateControl.Text;
                if (m_isTrimText)
                    str = str.Trim();

                if (m_notFuncIsValid)
                    return !ValidateFunc(str);

                return ValidateFunc(str);
            }

            return false;
        }
    }
}
